package com.xm.controller;


import com.xm.common.utils.JSONResult;
import com.xm.common.utils.JsonUtils;
import com.xm.common.utils.MD5Utils;
import com.xm.pojo.Users;
import com.xm.pojo.UsersReport;
import com.xm.service.UserService;
import com.xm.vo.PublisherVideo;
import com.xm.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.UUID;

@RestController()
@Api(value = "用户业务的接口",tags = {"用户业务的controller"})
@RequestMapping("/user")
public class UserController extends BasicController{


    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户上传头像",notes = "用户上传头像的接口")
    @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "String",paramType = "query")
    @PostMapping("/uploadFace")
    public JSONResult uploadFace(String userId, @RequestParam("file") MultipartFile[] files) throws Exception{
        if(StringUtils.isEmpty(userId)){
            return JSONResult.errorMsg("用户为空");
        }
        //文件保存的目录
        String filePath = "D:/毕设项目/video-file";
        //保存到数据库的文件相对路径
        String uploadPathDB = "/" + userId + "/face";
        FileOutputStream fileOutputStream = null;
        InputStream inputStream = null;
        try {
            if(files != null && files.length >  0){
                String fileName = files[0].getOriginalFilename();
                if(!StringUtils.isEmpty(fileName)){
                    //文件保存的绝对路径
                    String finalFacePath = filePath + uploadPathDB +"/" + fileName;
                    //数据库保存的路径
                    uploadPathDB += "/" + fileName;
                    File outFile = new File(finalFacePath);
                    if(outFile.getParentFile() != null || !outFile.getParentFile().isDirectory()){
                        //创建父文件夹
                        outFile.getParentFile().mkdirs();

                    }
                    fileOutputStream = new FileOutputStream(outFile);
                    inputStream = files[0].getInputStream();
                    IOUtils.copy(inputStream,fileOutputStream);
                }
            }else{
                return JSONResult.errorMsg("上传出错了...");
            }
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.errorMsg("上传出错了...");
        }finally {
            if(fileOutputStream != null){
                fileOutputStream.flush();;
                fileOutputStream.close();
            }
        }

        Users user = new Users();
        user.setId(userId);
        user.setFaceImage(uploadPathDB);
        userService.updataUserInfo(user);
        return JSONResult.ok(uploadPathDB);
    }

    @ApiOperation(value = "查询用户信息",notes = "查询用户信息的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "beFocusUserId",value = "被关注者id",required = false,dataType = "String",paramType = "query")


    })
    @PostMapping("/query")
    public JSONResult query(String userId,String fanId) throws Exception{
        if(StringUtils.isEmpty(userId)) {
            return JSONResult.errorMsg("用户为空");
        }
        Users user = userService.query(userId);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user,userVo);

        userVo.setFollow(userService.isFollow(fanId,userId));


        return JSONResult.ok(userVo);
    }

    @ApiOperation(value = "查询发布视频的用户信息",notes = "查询发布视频的用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginUserId",value = "用户id",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "videoId",value = "视频id",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "publisherUserId",value = "发布者id",required = true,dataType = "String",paramType = "query")
    })
    @PostMapping("/queryPublisher")
    public JSONResult queryPublisher(String loginUserId,String videoId,String publisherUserId) throws Exception{
        if(StringUtils.isEmpty(publisherUserId)) {
            return JSONResult.errorMsg("空空如也...");
        }
        //查询发布者的信息
        Users userInfo = userService.query(publisherUserId);
        UserVo publisher = new UserVo();
        BeanUtils.copyProperties(userInfo,publisher);
        //查询登录用户与视频的点赞关系
        boolean isLike = userService.isUserLikeVideos(loginUserId,videoId);
        PublisherVideo publisherVideo = new PublisherVideo();
        publisherVideo.setIsLike(isLike);
        publisherVideo.setUserVo(publisher);
        return JSONResult.ok(publisherVideo);
    }

    @ApiOperation(value = "关注用户",notes = "关注用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginUserId",value = "登录用户id",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "beFocusUserId",value = "被关注用户id",required = true,dataType = "String",paramType = "query"),

    })
    @PostMapping("/userFollow")
    public JSONResult userFollow(String loginUserId,String beFocusUserId) throws Exception{
        if(StringUtils.isEmpty(beFocusUserId)) {
            return JSONResult.errorMsg("空空如也...");
        }
        userService.FocusOnUser(loginUserId,beFocusUserId);
        return JSONResult.ok();
    }

    @ApiOperation(value = "取消关注用户",notes = "取消关注用户接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginUserId",value = "登录用户id",required = true,dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "beFocusUserId",value = "被关注用户id",required = true,dataType = "String",paramType = "query"),

    })
    @PostMapping("/userUnFollow")
    public JSONResult userUnFollow(String loginUserId,String beFocusUserId) throws Exception{
        if(StringUtils.isEmpty(beFocusUserId)) {
            return JSONResult.errorMsg("空空如也...");
        }
        userService.UnFocusOnUser(loginUserId,beFocusUserId);
        return JSONResult.ok();
    }

    @ApiOperation(value = "举报用户",notes = "举报用户的接口")
    @ApiImplicitParam(name = "usersReport",value = "举报信息对象",required = true,dataType = "String",paramType = "body")
    @PostMapping("/reportuser")
    public JSONResult reportUser(@RequestBody UsersReport usersReport) throws Exception{
        //保存举报信息
        userService.reportUser(usersReport);
        return JSONResult.ok("举报成功!!");
    }



    /*public UserVo setRedisSessionToken(Users users){
        String userToken = UUID.randomUUID().toString();
        redis.set(USER_REDIS_SESSION + ":" + users.getId(),userToken,1000*30*60);
        UserVo userVo = new UserVo();
        userVo.setUserToken(userToken);
        BeanUtils.copyProperties(users,userVo);
        return userVo;
    }*/
}
